package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.ClassifyActivity;
import com.lsy.weizhi2.myutils.HomeNotesCall;
import com.lsy.weizhi2.utils.MyMethod;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/14.
 */
public class HomeFragmentDown extends Fragment implements View.OnClickListener,ViewPager.OnPageChangeListener{
    private Context context;
    private TextView hot_note,recommend_note,reward_note,buy_note,sell_note,notes_add;
    private View hot_line,recommend_line,reward_line,buy_line,sell_line;
    private List<View>list_v;
    private ViewPager home_notes_vp;
    private FragmentManager fragmentManager;
    private Home_Hot home_hot;
    private Home_Recommend home_recommend;
    private Home_Reward home_reward;
    private Home_Buy home_buy;
    private Home_Sell home_sell;
    private List<Fragment> list_f;
    private PageAdapter adapter;
    private HomeNotesCall homeNotesCall;
    private int width;
    private int index;
    private RelativeLayout.LayoutParams params;
    private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.home_fragment_down,container,false);
        hot_note= (TextView) view.findViewById(R.id.hot_note);
        recommend_note= (TextView) view.findViewById(R.id.recommend_note);
        reward_note= (TextView) view.findViewById(R.id.reward_note);
        buy_note= (TextView) view.findViewById(R.id.buy_note);
        sell_note= (TextView) view.findViewById(R.id.sell_note);
        hot_line= view.findViewById(R.id.hot_line);
        recommend_line= view.findViewById(R.id.recommend_line);
        reward_line= view.findViewById(R.id.reward_line);
        buy_line= view.findViewById(R.id.buy_line);
        sell_line= view.findViewById(R.id.sell_line);
        notes_add= (TextView) view.findViewById(R.id.notes_add);
        home_notes_vp= (ViewPager) view.findViewById(R.id.home_notes_vp);
        list_v=new ArrayList<>();
        list_v.add(hot_line);
        list_v.add(recommend_line);
        list_v.add(reward_line);
        list_v.add(buy_line);
        list_v.add(sell_line);
        AutoUtils.autoSize(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();

        fragmentManager=getChildFragmentManager();
        list_f=new ArrayList<>();
        home_hot=new Home_Hot();
        home_recommend=new Home_Recommend();
        home_reward=new Home_Reward();
        home_buy=new Home_Buy();
        home_sell=new Home_Sell();
        list_f.add(home_hot);
        list_f.add(home_recommend);
        list_f.add(home_reward);
        list_f.add(home_buy);
        list_f.add(home_sell);
        adapter=new PageAdapter(fragmentManager);
        home_notes_vp.setAdapter(adapter);
        home_notes_vp.setCurrentItem(index);
        setSell_note(index);
        home_notes_vp.addOnPageChangeListener(this);
        hot_note.setOnClickListener(this);
        recommend_note.setOnClickListener(this);
        reward_note.setOnClickListener(this);
        buy_note.setOnClickListener(this);
        sell_note.setOnClickListener(this);
        notes_add.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("ParentView","home_notes_vp=down="+home_notes_vp);
    }

    public void setPosition(int index){
        this.index=index;
    }

    public void setHomeNotesCall(HomeNotesCall homeNotesCall){
        this.homeNotesCall=homeNotesCall;
    }

    private void setShowLine(int position){
        for (int i = 0; i < list_v.size(); i++) {
            if(i==position){
                list_v.get(i).setVisibility(View.VISIBLE);
            }else {
                list_v.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }
    public void setSell_note(int position){
        HideAll();
        home_notes_vp.setCurrentItem(position);
        switch (position){
            case 0:
                hot_note.setTextColor(getResources().getColor(R.color.clicked_ok));
                break;
            case 1:
                recommend_note.setTextColor(getResources().getColor(R.color.clicked_ok));
                break;
            case 2:
                reward_note.setTextColor(getResources().getColor(R.color.clicked_ok));
                break;
            case 3:
                buy_note.setTextColor(getResources().getColor(R.color.clicked_ok));
                break;
            case 4:
                sell_note.setTextColor(getResources().getColor(R.color.clicked_ok));
                break;
        }
    }

    private void HideAll(){
        hot_note.setTextColor(getResources().getColor(R.color.login_text_hint));
        recommend_note.setTextColor(getResources().getColor(R.color.login_text_hint));
        reward_note.setTextColor(getResources().getColor(R.color.login_text_hint));
        buy_note.setTextColor(getResources().getColor(R.color.login_text_hint));
        sell_note.setTextColor(getResources().getColor(R.color.login_text_hint));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hot_note:
                home_notes_vp.setCurrentItem(0);
                break;
            case R.id.recommend_note:
                home_notes_vp.setCurrentItem(1);
                break;
            case R.id.reward_note:
                home_notes_vp.setCurrentItem(2);
                break;
            case R.id.buy_note:
                home_notes_vp.setCurrentItem(3);
                break;
            case R.id.sell_note:
                home_notes_vp.setCurrentItem(4);
                break;
            case R.id.notes_add:
                MyMethod.toActivity(context,ClassifyActivity.class,new String[]{"position"},new int[]{100});
                break;
        }
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setSell_note(position);
        setShowLine(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class PageAdapter extends FragmentPagerAdapter {

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return list_f.get(position);
        }

        @Override
        public int getCount() {
            return list_f.size();
        }
    }
}
